#include <iostream>
#include <memory>

class Light
{
public:
    void on() { std::cout << "灯打开了\n"; }
    void off() { std::cout << "灯关闭了\n"; }
};

class Command
{
public:
    virtual ~Command() = default;
    virtual void execute() = 0;
};

class LightOnCommand : public Command
{
    Light &light;

public:
    LightOnCommand(Light &l) : light(l) {}
    void execute() override
    {
        light.on();
    }
};

class LightOffCommand : public Command
{
    Light &light;

public:
    LightOffCommand(Light &l) : light(l) {}
    void execute() override
    {
        light.off();
    }
};

class RemoteControl
{
    std::shared_ptr<Command> command;

public:
    void setCommand(std::shared_ptr<Command> cmd)
    {
        command = cmd;
    }
    void pressButton()
    {
        if (command)
            command->execute();
    }
};

int main()
{
    Light light;

    auto lightOn = std::make_shared<LightOnCommand>(light);
    auto lightOff = std::make_shared<LightOffCommand>(light);

    RemoteControl remote;
    remote.setCommand(lightOn);
    remote.pressButton(); // 灯打开了

    remote.setCommand(lightOff);
    remote.pressButton(); // 灯关闭了
}
